package com.mq.test.config.mq.listener;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MQConsumeListeners {
    private static Logger logger = LoggerFactory.getLogger(MQConsumeListeners.class);
    @RabbitListener(queues={"queue.name"})
    public void testConsume(Message message, Channel channel, String message1){
/*        try {
           // channel.basicQos(1);
            if(1==2) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            }
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        logger.info("mq consume :"+ message);
    }
    // testConsume 还可以写成如下形式，使用基本数据类型直接接受message的body
//    @RabbitListener(queues={"queue.name"})
    public void testConsume2(String data, Channel channel, String message1){
        logger.info(("接受到的数据:"+data));

    }
    @RabbitListener(queues={"queue.test"})
    public void testConsume3(Message message, Channel channel, String message1){
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            logger.info("ack:"+message.getMessageProperties().getDeliveryTag());

        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.info("mq consume3 :"+ message);
    }
}
